#include "mainwindow.h"

#include "myformdoc.h"
#include "qmdisubwindow.h"
#include "qfiledialog.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);
    setCentralWidget(ui->mdiArea);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_New_triggered()
{
    MyFormDoc* form_doc = new MyFormDoc(this);
    ui->mdiArea->addSubWindow(form_doc);
    form_doc->show();

    ui->action_Cut->setEnabled(true);
    ui->action_Copy->setEnabled(true);
    ui->action_Paste->setEnabled(true);
    ui->action_Font->setEnabled(true);
}

void MainWindow::on_action_Open_triggered()
{
    /*打开文件时，如果存在新建的空窗口，且此窗口活动
    则直接在活动的空窗口中打开，不再新建窗口*/

    bool need_new = false;//是否需要新建窗口
    MyFormDoc* form_doc = nullptr;
    
    if (ui->mdiArea->subWindowList().size() > 0)//如果多文档窗体中存在窗口
    {
        //看看当前活动窗口是不是已经打开了文件
        form_doc = dynamic_cast<MyFormDoc*> (ui->mdiArea->activeSubWindow()->widget());
        need_new = form_doc->is_file_opened();
    }
    else//多文档窗体中不存在窗口
    {
        need_new = true;//需要新建
    }

    /*QString path_now = QCoreApplication::applicationDirPath();
    QString file_name = QFileDialog::getOpenFileName(this, "打开一个文件", path_now,
        "C/C++(*.h *.c *.cpp *.hpp);; 文本文件(*.txt);; 所有文件(*.*)");*/

    QString file_name = QFileDialog::getOpenFileName(this, "打开一个文件", "C:\\Users\\CaiYunWei\\Desktop",
        "C/C++(*.h *.c *.cpp *.hpp);; 文本文件(*.txt);; 所有文件(*.*)");

    if (file_name.isEmpty())
        return;

    if (need_new)
    {
        form_doc = new MyFormDoc(this);
        ui->mdiArea->addSubWindow(form_doc);
    }

    form_doc->load_from_file(file_name);
    form_doc->show();

    ui->action_Cut->setEnabled(true);
    ui->action_Copy->setEnabled(true);
    ui->action_Paste->setEnabled(true);
    ui->action_Font->setEnabled(true);

}

void MainWindow::on_action_Font_triggered()
{
    MyFormDoc* form_doc = dynamic_cast<MyFormDoc*>(ui->mdiArea->activeSubWindow()->widget());//当前活动窗口
    if (form_doc == nullptr) return;
    form_doc->set_edit_font();

}

void MainWindow::on_action_Cut_triggered()
{
    MyFormDoc* form_doc = dynamic_cast<MyFormDoc*>(ui->mdiArea->activeSubWindow()->widget());//当前活动窗口
    if (form_doc == nullptr) return;
    form_doc->text_cut();
}

void MainWindow::on_action_Copy_triggered()
{
    MyFormDoc* form_doc = dynamic_cast<MyFormDoc*>(ui->mdiArea->activeSubWindow()->widget());//当前活动窗口
    if (form_doc == nullptr) return;
    form_doc->text_copy();
}

void MainWindow::on_action_Paste_triggered()
{
    MyFormDoc* form_doc = dynamic_cast<MyFormDoc*>(ui->mdiArea->activeSubWindow()->widget());//当前活动窗口
    if (form_doc == nullptr) return;
    form_doc->text_paste();
}

void MainWindow::on_action_MDI_triggered(bool checked)
{
    if (checked)
        ui->mdiArea->setViewMode(QMdiArea::TabbedView);
    else
        ui->mdiArea->setViewMode(QMdiArea::SubWindowView);

    ui->mdiArea->setTabsClosable(checked);


    ui->action_Cascade->setEnabled(!checked);
    ui->action_Tile->setEnabled(!checked);
}

void MainWindow::on_action_Cascade_triggered()
{
    ui->mdiArea->cascadeSubWindows();
}

void MainWindow::on_action_Tile_triggered()
{
    ui->mdiArea->tileSubWindows();
}

void MainWindow::on_action_CloseAll_triggered()
{
    ui->mdiArea->closeAllSubWindows();
}

void MainWindow::on_action_Save_triggered()
{
    MyFormDoc* form_doc = dynamic_cast<MyFormDoc*>(ui->mdiArea->activeSubWindow()->widget());//当前活动窗口
    if (form_doc == nullptr) return;
    form_doc->save_to_file();
}

void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow* window)
{
    if (ui->mdiArea->subWindowList().size() == 0)
    {//禁止相关行为
        ui->action_Cut->setEnabled(false);
        ui->action_Copy->setEnabled(false);
        ui->action_Paste->setEnabled(false);
        ui->action_Font->setEnabled(false);
    }
}


